﻿begin;

select s.*
into tmp_weatherstation
from weatherstation s, 
(select max(id) id
from weatherstation
group by primary_id) x
where s.id = x.id;

delete from weatherstation;

insert into weatherstation (select * from tmp_weatherstation);

drop table tmp_weatherstation;

commit;